package cn.wolfcode.trip.base.service.impl;

import cn.wolfcode.trip.base.domain.News;
import cn.wolfcode.trip.base.domain.NewsCommend;
import cn.wolfcode.trip.base.mapper.NewsCommendMapper;
import cn.wolfcode.trip.base.mapper.NewsMapper;
import cn.wolfcode.trip.base.query.NewsCommendQueryObject;
import cn.wolfcode.trip.base.query.NewsQueryObject;
import cn.wolfcode.trip.base.service.INewsCommendService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class NewsCommendServiceImpl implements INewsCommendService {
    @Autowired
    private NewsCommendMapper newsCommendMapper;
@Autowired
private NewsMapper newsMapper;
    @Override
    public PageInfo<NewsCommend> query(NewsCommendQueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize(), qo.getOrderBy());
        List<NewsCommend> list = newsCommendMapper.queryForList(qo);
        return new PageInfo<>(list);
    }

    @Override
    public void saveOrUpdate(NewsCommend newsCommend) {
        News news = newsMapper.selectByPrimaryKey(newsCommend.getNews().id);
        news.setState(1);
        newsMapper.changeState(news);
        newsCommend.setSchedule(new Date());
        newsCommendMapper.insert(newsCommend);

    }
}
